<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">维修完成</block>
			</cu-custom>
			<!-- 数据列表 -->
			<form @submit="handleSubmitZS" v-if="repairType === 'zs'">
				<view class="cu-form-group">
					<view class="title">不合格原因：</view>
					<input placeholder="请输入不合格原因" name="unqualifiedReason"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">维修方案：</view>
					<input placeholder="请输入维修方案" name="repairProgramme"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">维修人员：</view>
					<input placeholder="请输入维修人员" name="repairPersonnel"></input>
				</view>
				<button form-type="submit" class="btns cu-btn bg-rgb lg animation-fade">提交完成</button>
			</form>
			<form @submit="handleSubmitYZY" v-else>
				<!-- 查询条件 -->
				<view class="fixed" :style="[{animationDelay: 0.1 + 's'}]">
					<form>
						<view class="cu-form-group last">
							<image class="icon" src="../../static/saomiao.png" mode=""></image>
							<input auto-focus placeholder="请扫描制令单号" name="input" v-model="code"
								@confirm="inputQuery"></input>
						</view>
					</form>
				</view>
				<view class="cu-form-group" style="margin-top: 100rpx;">
					<view class="title">线别：{{mesInfo ? mesInfo.lineType : ''}}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">站别：</view>
					<!-- <input placeholder="请输入站别" name="standType"></input> -->
					<picker name="standType" dataType="1" @change="PickerChange" :value="index" :range="typeArray">
						<view class="picker">
							{{line}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">机型：</view>
					<input placeholder="请输入机型" name="model"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">不良数量：</view>
					<input placeholder="请输入不良数量" name="badQuantity"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">SN：</view>
					<input placeholder="请输入SN" name="correspondingBarcode"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">维修位置：</view>
					<input placeholder="请输入维修位置" name="repairProgramme"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">不良现象：</view>
					<input placeholder="请输入不良现象" name="unqualifiedReason"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">备注：</view>
					<input placeholder="请输入备注" name="query1"></input>
				</view>
				<button form-type="submit" class="btns cu-btn bg-rgb lg animation-fade">提交完成</button>
			</form>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				line:'请选择站别',
				index:'0',
				typeArray: [],
				checked: {
					type: '',
					project01: '',
					project02: '',
					project03: '',
					project04: '',
					project05: '',
					project06: '',
					project07: '',
					project08: '',
					project09: '',
					project10: '',
					project11: '',
					project12: '',
					project13: '',
					project14: '',
					project15: '',
					decide: '',
				},
				imglist: [],
				inspectGrade: "",
				materielCode: '',
				list: [],
				preservationList: [],
				objMsg: {},
				radioItems: [{
						value: '有铅',
						name: '有铅'
					},
					{
						value: '无铅',
						name: '无铅'
					},
					{
						value: '红胶',
						name: '红胶'
					}
				],
				radioCurrent: -1,
				resultRadios: [{
						value: 'OK',
						name: 'OK'
					},
					{
						value: 'NG',
						name: 'NG'
					}
				],
				resultRadioItems: [{
						value: '1',
						name: 'OK'
					},
					{
						value: '0',
						name: 'NG'
					}
				],
				resultRadioCurrent: -1,
				hasGrade: false,
				type: '1',
				title: {
					'1': '首件',
					'2': '巡件',
					'3': '末件',
				},
				commandbillCode: '',
				produceId: '',
				id: '',
				repairType: 'yzy',
				code:'',
				mesInfo:null,
			}
		},
		onLoad(e) {
			this.id = this.$Route.query.id
			this.repairType = this.$Route.query.repairType
			// this.getZhanbie()
			// this.objMsg = uni.getStorageSync('firstPieceQualityInspection');
			// console.log(this.objMsg)
			// this.type = this.$Route.query.type
			// this.qualityTesting()
		},
		methods: {
			inputQuery(){
				this.$http.get('produce/mesCommandbillInfo/queryByCommandBillCode',{
					params:{
						code:this.code
					}
				}).then(res => {
					if (res.data.success) {
						this.mesInfo = res.data.result
						this.getZhanbie()
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			PickerChange(e) {
			  this.index = e.target.value
			  this.line = this.typeArray[this.index]
			},
			getZhanbie() {
				this.$http.get('sys/dict/queryDictItemByDictCode',{
					params:{
						dictCode:'stand_by',
						produceGrade:this.mesInfo.produceGrade
					}
				}).then(res => {
					if (res.data.success) {
						this.line = '请选择站别'
						this.index = '0'
						this.typeArray = res.data.result[0].listValue
						console.log(111,this.typeArray)
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			takephoto() {
				let that = this
				let maxUpload = 1
				uni.chooseImage({
					count: 1, // 最多可以选择的图片张数，默认1
					sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ['camera', 'album'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
					success: (res) => {
						console.log(res);
						const tempFilePaths = res.tempFilePaths
						tempFilePaths.forEach(item => {
							uni.uploadFile({
								url: that.$http.config.baseUrl + 'sys/common/upload',
								filePath: item,
								method: 'POST',
								name: 'file',
								header: {
									'X-Access-Token': uni.getStorageSync('Access-Token')
								},
								success: function(uploadFileRes) {
									that.imglist = []
									that.modal.show({
										type: 'success',
										content: '上传成功!'
									})
									let result = JSON.parse(uploadFileRes.data)
									if (result.success) {
										that.imglist = that.imglist.concat([result
											.message
										]); //concat追加到数组
									} else {
										that.modal.show({
											type: 'error',
											content: result.message
										})
									}
								}
							});
						})
					}
				});
			},
			checkChange(item, evt) {
				item.insLog = evt.target.value
			},
			radioChange: function(type, cur, evt) {
				for (let i = 0; i < this[type].length; i++) {
					if (this[type][i].value === evt.target.value) {
						this.cur = i;
						break;
					}
				}
			},
			checkboxChange: function(e) {
				var items = this.checkBoxItems,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if (values.includes(item.value)) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				}
			},
			qualityTesting() {
				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				let data = {
					inspectGrade: '201',
					materielCode: this.objMsg.mechanismCode
				}
				this.$http.get('inspect/mesMaterielCheckprojrct/getByGrade', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.list = res.data.result
						if (this.list.length > 0) {
							this.hasGrade = true
							for (let i = 0; i < this.list.length; i++) {
								let data = {
									insLog: '',
									badCauses: '',
									insItem: this.list[i].projectName,
									// projectCode:this.list[i].projectCode
								}
								this.preservationList.push(data)
							}
							console.log(1111, this.preservationList)
						}
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					}
				})
			},
			handleSubmitZS(e) {
				let {
					unqualifiedReason,
					repairProgramme,
					repairPersonnel
				} = e.detail.value
				let obj = {
					id: this.id,
					status: 'waiting_retest',
					...e.detail.value
				}
				console.log(111, obj)
				if (unqualifiedReason.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入不合格原因'
					})
					return
				}
				if (repairProgramme.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入维修方案'
					})
					return
				}
				if (repairPersonnel.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入维修人员'
					})
					return
				}

				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				this.$http.post('inspect/zsUnqualifiedRepairCard/edit', obj).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
						setTimeout(function() {
							uni.$emit('updateRepair')
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			handleSubmitYZY(e) {
				if (!this.mesInfo) {
					this.modal.show({
						type: 'error',
						content: '请先扫描制令单号'
					})
					return
				}
				if (!this.line || this.line === '请选择站别') {
					this.modal.show({
						type: 'error',
						content: '请选择站别'
					})
					return
				}
				let {
					lineType,
					standType,
					model,
					badQuantity,
					correspondingBarcode,
					unqualifiedReason,
					repairProgramme,
					query1
				} = e.detail.value
				let obj = {
					id: this.id,
					commandbillCode:this.code,
					produceId:this.mesInfo.produceId,
					type: 'repair_yzy',
					status: 'confirm_complete',
					...e.detail.value
				}
				obj.lineType = this.mesInfo.lineType
				obj.standType = this.line
				console.log(111, obj)
				if (obj.lineType.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入线别'
					})
					return
				}
				if (obj.standType.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入站别'
					})
					return
				}
				if (model.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入机型'
					})
					return
				}
				if (badQuantity.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入不良数量'
					})
					return
				}
				if (correspondingBarcode.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入或扫描条码'
					})
					return
				}
				if (unqualifiedReason.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入不良现象'
					})
					return
				}
				if (repairProgramme.trim().length === 0) {
					this.modal.show({
						type: 'error',
						content: '请输入维修位置'
					})
					return
				}

				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				let url = this.id ? 'inspect/zsUnqualifiedRepairCard/edit' : 'inspect/zsUnqualifiedRepairCard/add'
				this.$http.post(url, obj).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
						setTimeout(function() {
							uni.$emit('updateRepair')
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			onSubmitPrin() {
				uni.showModal({
					title: '提示',
					content: '本次质检是否合格',
					cancelText: '不合格',
					confirmText: '合格',
					success: res => {
						if (res.confirm) {
							uni.showModal({
								title: '保存首件质检',
								content: '是否要保存本次质检合格记录？',
								cancelText: '取消',
								confirmText: '确定',
								success: res => {
									if (res.confirm) {
										uni.showLoading({
											mask: true,
											title: '加载中.....'
										})
										let obj = {
											inspectGrade: this.objMsg.produceGrade,
											materielCode: this.objMsg.mechanismCode,
											materielName: this.objMsg.mechanismName,
											relatedCode: this.objMsg.id,
											inspectQualify: '是',
											mesBadnessInfoList: []
										}
										for (let i = 0; i < this.preservationList.length; i++) {
											let data = {
												inspectProject: this.preservationList[i].code +
													'-' + this.preservationList[i].name,
												inspectResult: this.preservationList[i].txt,
												query2: this.preservationList[i].num
											}
											obj.mesBadnessInfoList.push(data);
										}
										this.$http.post('inspect/mesBadnessHandle/appAdd', obj)
											.then(res => {
												uni.hideLoading()
												if (res.data.success) {
													// this.$tip.success(res.data.message);
													this.modal.show({
														type: 'success',
														content: res.data.message
													})
													setTimeout(function() {
														uni.navigateBack({
															delta: 1
														})
													}, 1000);
												} else {
													// this.$tip.error(res.data.message)
													this.modal.show({
														type: 'error',
														content: res.data.message
													})
												}
											})
									}
								}
							})
						} else {
							uni.showModal({
								title: '保存首件质检',
								content: '是否要保存本次质检不合格记录？',
								cancelText: '取消',
								confirmText: '确定',
								success: res => {
									if (res.confirm) {
										uni.showLoading({
											mask: true,
											title: '加载中.....'
										})
										let obj = {
											inspectGrade: this.objMsg.produceGrade,
											materielCode: this.objMsg.mechanismCode,
											materielName: this.objMsg.mechanismName,
											relatedCode: this.objMsg.id,
											inspectQualify: '否',
											mesBadnessInfoList: []
										}
										for (let i = 0; i < this.preservationList.length; i++) {
											let data = {
												inspectProject: this.preservationList[i].code +
													'-' + this.preservationList[i].name,
												inspectResult: this.preservationList[i].txt,
												query2: this.preservationList[i].num
											}
											obj.mesBadnessInfoList.push(data);
										}
										this.$http.post('inspect/mesBadnessHandle/appAdd', obj)
											.then(res => {
												uni.hideLoading()
												if (res.data.success) {
													// this.$tip.success(res.data.message);
													this.modal.show({
														type: 'success',
														content: res.data.message
													})
													setTimeout(function() {
														uni.navigateBack({
															delta: 1
														})
													}, 1000);
												} else {
													// this.$tip.error(res.data.message)
													this.modal.show({
														type: 'error',
														content: res.data.message
													})
												}
											})
									}
								}
							})
						}
					}
				})
			},
		},
	}
</script>

<style lang="less">
	.page {
		padding-bottom: 80rpx;
		
		.fixed {
			position: fixed;
			z-index: 1;
			width: 100%;
			background-color: white;
			height: 100rpx;
			border-bottom: 1rpx solid #eee;
		
			.last {
				border-bottom: 1rpx solid #eee;
				position: relative;
		
				.icon {
					width: 30rpx;
					height: 29rpx;
					position: absolute;
					top: 36rpx;
					left: 60rpx;
				}
		
				input {
					width: 686rpx;
					height: 68rpx;
					background: #FFFFFF;
					border: 1px solid #D5D8DF;
					border-radius: 34px;
					padding-left: 79rpx;
				}
			}
		}
	}

	.borderTop {
		border-top: 0.5px solid #eee;
	}

	.border {
		border: 0.5px solid #eee;
	}

	.checkBoxWrap.cu-form-group+.cu-form-group {
		border: 0;
	}

	.btns {
		position: fixed;
		z-index: 999;
		background: white;
		bottom: 0;
		height: 80rpx;
		line-height: 100rpx;
	}

	.cu-btn {
		width: 650rpx;
		margin-left: 50rpx;
	}

	// .card {
	// 	margin: 30rpx;
	// 	box-shadow: 0px 0px 5rpx 2rpx rgba(0, 0, 0, 0.1);
	// 	border-radius: 10rpx;
	// 	padding: 30rpx;
	// 	background-color: white;
	// }

	// .card .top {
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: space-between;
	// 	border-bottom: 1rpx solid #F0F0F0;
	// 	padding-bottom: 20rpx;
	// 	font-weight: bold;
	// }

	// .card .bottom .box {
	// 	display: flex;
	// 	justify-content: space-between;
	// 	padding: 20rpx 0;
	// }

	// .card .bottom .box .lable {
	// 	width: 25%;
	// }

	// .card .bottom .box .content {
	// 	width: 74%;
	// 	text-align: right;
	// }
	// .boxs{
	// 	display: flex;
	// 	justify-content: space-between;
	// 	padding: 20rpx 0;
	// 	align-items: center;
	// }
	// // .boxs .title{
	// // 	font-weight: bold;
	// // 	padding: 10rpx;
	// // 	border-radius: 10rpx;
	// // 	color: white;;
	// // }
	// .boxs input {
	// 	border: 2rpx solid red;
	// 	height: 60rpx;
	// 	padding: 10rpx;
	// 	border-radius: 10rpx;
	// 	font-size: 20rpx;
	// 	color: red;
	// }
	// .boxs input .input-placeholder{
	// 	color: red;
	// }
	// uni-textarea{
	// 	border: 2rpx solid #F0F0F0;
	// 	padding: 10rpx;
	// 	border-radius: 10rpx;
	// 	font-size: 20rpx;
	// }
	// .list{
	// 	margin-bottom: 120rpx;
	// }
	// .btns{
	// 	position: fixed;
	// 	z-index: 999;
	// 	background: white;
	// 	bottom: 0;
	// 	height: 100rpx;
	// 	line-height: 100rpx;
	// }
	// .cu-btn{
	// 	width: 650rpx;
	// 	margin-left: 50rpx;
	// }
	.picture-box {
		width: 100%;
		margin-top: 10px;

		.picture {
			width: 100%;
			height: 200px;
			margin-bottom: 10px;
		}
	}
</style>
